<?php
/**
 * @Name: UserModel.php
 * @Author: yashuai
 */

namespace App\Http\Model\Rbac;

use App\Http\Model\ConstCode;

class UserModel extends BaseModel
{

    protected $table = 'admin_user';

    /**
     * 关联用户角色
     */
    public function role()
    {
        return $this->hasMany(UserRoleModel::class, 'UserID', 'UserID')
            ->select(['UserID', 'RoleID']);
    }

    /**
     * @Desc 验证用户是否存在
     * @param int $userId
     * @param bool $status 是否验证状态
     * @param string $select
     * @return array|\Illuminate\Database\Query\Builder|mixed
     * @throws \App\Exceptions\ApiException
     * @author yashuai
     */
    public static function userExist($userId = 0, $status = false, $select='*')
    {
        $info = self::getInfo($userId, 'UserID', $select);
        if (empty($info) || ($status && $info['UserStatus'] != ConstCode::BASIC_STATUS_NORMAL)) {
            _throw(106001);
        }
        return $info;
    }

}
